//
//  TPUpdateTelViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/11/3.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPUpdateTelViewController.h"

@interface TPUpdateTelViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *oldTextField;
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UITextField *suqTextField;
@property (nonatomic,strong)UILabel *phoneLabel;
@property (nonatomic,assign)BOOL isSet;

@end

@implementation TPUpdateTelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isSet = [LoginModel shareLoginModel].mobile.length == 0;
    self.title = _isSet ? @"绑定手机号": @"修改手机号";
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0.0);
        make.top.mas_equalTo(IOS7_TOP_Y);
    }];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT - IOS7_TOP_Y)];
    view.userInteractionEnabled = YES;
    view.clipsToBounds = YES;
    [scrollView addSubview:view];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:ImageNamed(@"TP_login_update_phone_header")];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(HeightScaleSize(20));
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(222, 117));
    }];
    
    UILabel *nameLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(14) color:TPAllBackgroundColor aligment:NSTextAlignmentCenter];
    nameLabel.text = _isSet ? @"当前未绑定手机":@"当前绑定";
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view);
        make.top.mas_equalTo(imageView.mas_bottom);
        make.height.mas_equalTo(30.0);
    }];
    
    _phoneLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(14) color:TPBlack_666666 aligment:NSTextAlignmentCenter];
    _phoneLabel.text = xw_getNULLString([LoginModel shareLoginModel].mobile);
    _phoneLabel.hidden = _isSet;
    [view addSubview:_phoneLabel];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(nameLabel);
        make.top.mas_equalTo(nameLabel.mas_bottom);
        make.height.mas_equalTo(20.0);
    }];
    
    
    
    _oldTextField = [self setup_placeholder:@"请输入姓名" leftImage:@"TP_login_name_icon" subView:view];
    _oldTextField.text = xw_getNULLString([LoginModel shareLoginModel].user_name);
    
    [_oldTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_phoneLabel.mas_bottom).offset(40.0);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.mas_equalTo( 38.0f);
        make.centerX.mas_equalTo(self.view);
    }];
    
    _textField = [self setup_placeholder:@"请输入手机号码" leftImage:@"TP_login_update_phone_icon" subView:view];
    UIButton *codeButton = [_textField xw_addRightCodeViewFrame:CGRectMake(0, 0, 90, 38.0) string:@"获取验证码" btnFrame:CGRectMake(0,5,90.0,28) color:TPAllBackgroundColor];
    [codeButton addTouchUpInsideTarget:self action:@selector(getCodeButton:)];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oldTextField .mas_bottom).offset(20.0);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.8);
        make.height.mas_equalTo(38.0f);
        make.centerX.mas_equalTo(self.view);
    }];
    
    _suqTextField = [self setup_placeholder:@"请输入验证码" leftImage:@"TP_login_code_icon" subView:view];
    [_suqTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textField.mas_bottom).offset(20.0);
        make.width.height.mas_equalTo(self.textField);
        make.centerX.mas_equalTo(self.view);
    }];
    
    
    @weakify(self);
    TPButton *submitButton = [TPButton buttonWithTitle:_isSet ? @"立即绑定" : @"修改绑定" toHeight:38.0 ActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
        
        [self.view endEditing:YES];
        [self getRequest];
        
    }];
    [submitButton tp_arrow];
    [view addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_suqTextField .mas_bottom).offset(50.0);
        make.height.mas_equalTo(38.0f);
        make.width.mas_equalTo(self.view).multipliedBy(0.5);
        make.centerX.mas_equalTo(self.view);
    }];
    
}

-(void)getCodeButton:(UIButton *)sender{
    
    [[TPRequestClient sharedClient]getRequestCodeButton:sender mobile:self.textField.text toType:_isSet ? LoginCodeStyleBindMobile:LoginCodeStyleEditMobile];
}
-(void)getRequest{
    
    [UIView xw_showHUDWithTitle:@"请求中..."];
    
    [[TPRequestClient sharedClient]getEditPhone:self.textField.text code:self.suqTextField .text user_name:self.oldTextField .text success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        
        [LoginModel shareLoginModel].mobile = _textField .text;
        [LoginModel shareLoginModel].user_name = self.oldTextField .text;
        [[LoginModel shareLoginModel]save];
        if(self.updateSccuessBlock){
            self.updateSccuessBlock(_textField .text);
        }
        
        [UIView xw_hideTitle:@"修改成功!!"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        
        [UIView xw_hideTitle:respone.message];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [UIView xw_hideTitle:@"Network Error"];
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //    [self.TextField becomeFirstResponder];
}
-(UITextField *)setup_placeholder:(NSString *)placeholder leftImage:(NSString *)image subView:(UIView *)subView{
    UITextField*textField = [UITextField initWithFrame:CGRectZero borderStyle:UITextBorderStyleNone fontSize:16 placeholder:placeholder text:@""];
    textField .delegate = self;
    
    [textField xw_addLeftImageViewFrame:CGRectMake(0, 0, 40.0, 40.0) image:[image isEqualToString:@"TP_login_name_icon"]?[ImageNamed(@"TP_login_name_icon") imageWithTintColor:TPAllBackgroundColor]: ImageNamed(image) imageFrame:CGRectMake(10, 10, 20.0, 20.0)];
//    [TextField setPlaceholderLabelTextColor:[UIColor whiteColor]];
    textField .textColor = [UIColor blackColor];
//    textField .secureTextEntry = YES;
    [textField tp_addLineBottomWithColor:TP_COLOR_245];
    [subView addSubview:textField ];
    return textField ;
    
}

@end
